<?php
namespace Kxdr\Controller;
use Think\Controller;
class GiftController extends ExtendController {
		public function index(){
			Vendor('Page');
			$curtp = I("tp");
			$trash = I("trash");
			$q = I("q");
			$where = "`sid`=".$this->sid;
			$where .= $curtp?" AND `type`=".$curtp:"";
			$where .= $trash?" AND `status`=9":" AND `status`=0";
			$where .=$q?" AND `name` LIKE '%".$q."%'":"";
			$GIFT = M("Gifts");
			$total = $GIFT->where($where)->count();
			$Page = new \Page($total,20);
			$show = $Page->show();
			
			$gifts = $GIFT->where($where)->limit($Page->firstRow.','.$Page->listRows)->getField("`id`, `type`, `level`, `status`, `name`,  `price`, `quantity`, `quantity_initial`, `pic_url`,  `tagid`,`cfg`");
			foreach($gifts as $k=>$gift){
				foreach(C("GFT_TYPE") as $tp){
					if($tp["id"]==$gift["type"]){
						$gifts[$k]["type_name"]=$tp["name"];
						break;
					}
				}
			}
			$this->assign("curtp",$curtp);
			$this->assign("trash",$trash);
			$this->assign("gift_type",C("GFT_TYPE"));
			$this->assign("gifts",$gifts);
			$this->assign("show",$show);
			$this->display("index");
		}
		public function edit(){
			$gid = I("gid");
			$GIFT = M("Gifts");
			$gift = current($GIFT->where("`sid`=".$this->sid." AND `id`=".$gid)->getField("`id`, `type`, `level`, `status`, `name`, `details`, `price`, `probability`, `isshow`, `quantity`, `quantity_initial`, `zailai`, `jifen`, `medal_id`, `medal_name`, `medal_pic`, `pic_url`, `link`, `num_iids`, `event_id`, `promoid`, `tagid`, `cfg`"));
			if(!$gift){
				$this->success("没有这个奖品！",U("index"));
				die();
			}
			$gift["cfg"]=json_decode($gift["cfg"]);
			if($gift["type"]==4){
				$KM = M("GiftsKm");
				$kmlist = $KM->where("`giftid`=".$gid." AND (`status`=0 OR `status`=9) AND `sid`=".$this->sid)->getField("`id`, `cardno`, `secret`,`status`");
				// session("kmlist".$gid,$kmlist);
			}
			foreach(C("GFT_TYPE") as $tp){
				if($tp["id"]==$gift["type"]){
					$curtp=$tp["name"];
					break;
				}
			}
			$this->assign("gid",$gid);
			$this->assign("kmlist",$kmlist);
			$this->assign("gift",$gift);
			$this->assign("curtp",$curtp);
			$this->assign("gift_type",C("GFT_TYPE"));
			$this->display();
		}
		public function save(){
			$GIFT = D("Gifts");
			$GIFT->picurl=I("picurl");
			$GIFT->giftname=I("giftname");
			$GIFT->gift_type=I("gift_type");
			$GIFT->lott=I("lott");
			$GIFT->jf=I("jf");
			$GIFT->promoid=I("promoid");
			$GIFT->tagid=I("tagid");
			$GIFT->medal_id=I("medal_id");
			$GIFT->medal_name=I("medal_name");
			$GIFT->medal_pic=I("medal_pic");
			$GIFT->gifturl=I("gifturl");
			$GIFT->price=I("price");
			$GIFT->quantity=I("quantity");
			$GIFT->kmlist=I("kmlist");
			$GIFT->detail=I("detail");
			$GIFT->sid=$this->sid;
			$GIFT->giftid=I("giftid");
			$reda = $GIFT->save_gift();
			if($reda){	
				header("Location: /?m=Kxdr&c=Gift");
				// $this->redirect("index");
			}else{
				$this->success("出错啦！","/?m=Kxdr&c=Gift");
			}
		}
		public function add_gift(){
			$GIFT = D("Gifts");
			$GIFT->picurl=I("picurl");
			$GIFT->giftname=I("giftname");
			$GIFT->gift_type=I("jplx");
			$GIFT->lott=I("lott");
			$GIFT->jf=I("jf");
			$GIFT->promoid=I("promoid");
			$GIFT->tagid=I("tagid");
			$GIFT->medal_id=I("medal_id");
			$GIFT->medal_name=I("medal_name");
			$GIFT->medal_pic=I("medal_pic");
			$GIFT->gifturl=I("gifturl");
			$GIFT->price=I("price");
			$GIFT->quantity=I("quantity");
			$GIFT->kmlist=I("kmlist");
			$GIFT->detail=I("detail");
			$GIFT->sid=$this->sid;
			$reda = $GIFT->add_gift();
			// $reda["hasError"]=true;
			$this->ajaxReturn($reda);
		}
		public function refond_gift(){
			$id = I("gid");
			$GIFT = M("Gifts");
			$re = $GIFT->where("`sid`=".$this->sid." AND `id`=".$id)->save(Array("status"=>0));
			if($re){
				$data["hasError"]=false;
			}else{
				$data["hasError"]=true;
				$data["error"]="没有这个奖品！";
			}
			$this->ajaxReturn($data);
		}
		public function remove_gift(){
			$id = I("gid");
			$GIFT = M("Gifts");
			$re = $GIFT->where("`sid`=".$this->sid." AND `id`=".$id)->save(Array("status"=>9));
			if($re){
				$data["hasError"]=false;
			}else{
				$data["hasError"]=true;
				$data["error"]="没有这个奖品！";
			}
			$this->ajaxReturn($data);
		}
		public function get_gift_types(){
			$types = C("GFT_TYPE");
			foreach($types as $tp){
				$data[]=Array("text"=>$tp["name"],"value"=>$tp["id"],"tip"=>$tp["tip"]);
			}
			$this->ajaxReturn($data);
		}
}